﻿<%@ WebHandler Language="C#" Class="xinxi_danwei" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class xinxi_danwei : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["danWeiType"];
            if (string.IsNullOrEmpty(requestType))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("请重试...");
                return;
            }
            else
            {
                switch (requestType)
                {
                    case "1":
                        Dictionary<string, string> orderBy = new Dictionary<string, string>();
                        orderBy.Add("danweileixing", "desc");
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_danwei", orderBy);
                        string jsonStr = JsonConvert.SerializeObject(Dt);
                        context.Response.Write(jsonStr);
                        break;
                    case "2":
                        string danWeiMingCheng = string.Format("{0}", context.Request.Form["danweimingcheng"]);
                        string danWeiLeiXing = string.Format("{0}", context.Request.Form["danweileixing"]);
                        string danWeiZhuangTai = string.Format("{0}", context.Request.Form["danweizhuangtai"]);
                        string danWeiID = string.Format("{0}", context.Request.Form["danWeiID"]);
                        string gongyingshang = string.Format("{0}", context.Request.Form["gongyingshang"]);
                        string gongyingshangmingcheng = string.Format("{0}", context.Request.Form["gongyingshangmingcheng"]);
                        if (danWeiLeiXing == "出库单位")
                        {
                            gongyingshang = "";
                            gongyingshangmingcheng = "";
                        }
                        if (string.IsNullOrEmpty(danWeiID))
                        {
                            string createCondition = m_DB.CreateAndCondition("danweimingcheng", DataValueCommonType.字符串, danWeiMingCheng, "", SearchConditionType.等于);
                            createCondition += m_DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, danWeiLeiXing, "", SearchConditionType.等于);
                            DataTable danWeiDt = m_DB.GetDataTableByCondition("tbl_danwei", createCondition);
                            if (danWeiDt.Rows.Count > 0)
                            {
                                context.Response.Write("已存在同名的单位，请检查。");
                                return;
                            }
                            //新增单位 
                            TableRow newRow = new TableRow("tbl_danwei", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol();
                            col1.ColName = "danweimingcheng";
                            col1.ValueMember = danWeiMingCheng;
                            col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "danweileixing";
                            col2.ValueMember = danWeiLeiXing;
                            col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "danweizhuangtai";
                            col3.ValueMember = danWeiZhuangTai;
                            col3.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "shangchuanzhuangtai";
                            col4.ValueMember = "1";
                            col4.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "zhandian";
                            col5.ValueMember = "云端";
                            col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "kehubianhao";
                            col6.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "gongyingshang";
                            col7.ValueMember = gongyingshang;
                            col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol();
                            col8.ColName = "gongyingshangmingcheng";
                            col8.ValueMember = gongyingshangmingcheng;
                            col8.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col8);

                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        else
                        {
                            //修改单位
                            string createCondition = m_DB.CreateAndCondition("danweimingcheng", DataValueCommonType.字符串, danWeiMingCheng, "", SearchConditionType.等于);
                            createCondition += m_DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, danWeiLeiXing, "", SearchConditionType.等于);
                            createCondition += m_DB.CreateAndCondition("id", DataValueCommonType.字符串, danWeiID, "", SearchConditionType.不等于);
                            DataTable danWeiDt = m_DB.GetDataTableByCondition("tbl_danwei", createCondition);
                            if (danWeiDt.Rows.Count > 0)
                            {
                                context.Response.Write("已存在同名的单位，请检查。");
                                return;
                            }
                            TableRow editRow = new TableRow("tbl_danwei", danWeiID, DBCtl.修改);
                            TableCol col1 = new TableCol();
                            col1.ColName = "danweimingcheng";
                            col1.ValueMember = danWeiMingCheng;
                            col1.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol();
                            col2.ColName = "danweileixing";
                            col2.ValueMember = danWeiLeiXing;
                            col2.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol();
                            col3.ColName = "danweizhuangtai";
                            col3.ValueMember = danWeiZhuangTai;
                            col3.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol();
                            col4.ColName = "shangchuanzhuangtai";
                            col4.ValueMember = "1";
                            col4.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol();
                            col5.ColName = "zhandian";
                            col5.ValueMember = "云端";
                            col5.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol();
                            col6.ColName = "kehubianhao";
                            col6.ValueMember = ConfigurationManager.AppSettings["客户编号"];
                            col6.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol();
                            col7.ColName = "gongyingshang";
                            col7.ValueMember = gongyingshang;
                            col7.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol();
                            col8.ColName = "gongyingshangmingcheng";
                            col8.ValueMember = gongyingshangmingcheng;
                            col8.ValueType = DataValueCommonType.字符串;
                            editRow.DataCols.Add(col8);

                            if (m_DB.SaveTableRows(editRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        break;
                    case "3":
                        //删除单位信息
                        string delDanWeiID = string.Format("{0}", context.Request.Form["danWeiID"]);
                        if (!string.IsNullOrEmpty(delDanWeiID))
                        {
                            TableRow delRow = new TableRow();
                            delRow.ID = delDanWeiID;
                            delRow.TableName = "tbl_danwei";
                            if (m_DB.DeleteRowByID(delRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        else
                        {
                            context.Response.Write("获取信息失败，请重试。");
                        }
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("单位信息异常。{0}", err.Message));
            context.Response.Write("请重试...");
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}